$(function(){
	var typeId = $(".typeid").val();
	
	var listArr = [{
        "sClass": "trhidden",
        "data": "id",
        "render": function (data, type, full, meta) {
            return '<input type="hidden"  class="checkchild"  value="' + data + '" id="checks' + data + '"/>';
        },
        "bSortable": false
    },{
    	"sClass": "text-left",
        "data": "keyName"
    },{
    	"sClass": "text-left",
        "data": "type"
    }];
	
	var permissionTable = function(){
		return matchTable('#datatables','/dictionary/listData.do',listArr, function(obj){},false);
	}
	
	permissionTable();
	
	
	 var listArrRam = [{
         "sClass": "text-center",
         "data": "id",
         "render": function (data, type, full, meta) {
             return '<input type="checkbox"  class="checkchild"  value="' + data + '" id="check' + data + '"/>';
         },
         "bSortable": false
     },{
     	"sClass": "text-left",
         "data": "keyName"
     },{
     	"sClass": "text-left",
         "data": "typeName"
     },{
     	"sClass": "text-left",
         "data": "typeKey"
     }];
     
     var dictionaryTable = function(obj){
     	return matchTable('#datatables_detail','/dictionary/listDet.do?type='+obj, listArrRam,function(obj){}, true);
     }
//    新增
    $(".permissopen").on('click',function(){
    	layer.open({
    	  title: '新增', 
		  skin: 'demo-class',
		  btn: ['确认', '取消'],
		  content: $(".permissform").html(),
		  success: function(layero, index){
			    console.log(layero, index);
			    $(layero).find('#addDicDetLeft').validate();
		  },
		  yes:function(index, layero){
			  if($(layero).find('#addDicDetLeft').valid()){
				  var treeid = $(layero).find('.treeidpara').val();
				  var jsonList = $(layero).find('form').serializeObject();
				  
				  ajaxSer.dictionLeftAdd(jsonList,function(data){
					  permissionTable(treeid).draw();
				  });
				  
				  //这里是回调，点击确认的时候执行的方法，最后一个关闭需要手动调用
				  layer.close(index);
			  }
		  },
		  cancel:function(index){
			  layer.close(index);
		  }
		});
    });
    
//  修改
    $(".permissedit").on('click',function(){
    	var choose_a = $('#datatables tbody .selected input');
        var c_tIds;
        c_tIds = choose_a.eq(0).val();
        if (c_tIds == undefined || $.trim(c_tIds) == "") {
        	layer.msg("请选择一条记录！");
            return false;
        }
        if(choose_a.length != 1){
        	layer.msg("只能选择一条记录！");
            return false;
        }
    	layer.open({
    	  title: '修改', 
  		  skin: 'demo-class',
  		  btn: ['确认', '取消'],
  		  content: $(".permissform").html(),
  		  success: function(layero, index){
  			  //修改之前执行填充操作
  			  $(layero).find('#addDicDetLeft').validate();
  			  ajaxSer.dictionLeftEditBefore(c_tIds,function(data){
	  				var nameVal = $(layero).find('form input');
					for(var i=0; i<nameVal.length;i++){
						if(data[$(nameVal[i]).attr('name')]){
							$(nameVal[i]).val(data[$(nameVal[i]).attr('name')])
						}
					}
			  });
  		  },
  		  yes:function(index, layero){
  			if($(layero).find('#addDicDetLeft').valid()){
  			  var treeid = $(layero).find('.treeidpara').val();
  			  var jsonList = $.extend({},{'Id':c_tIds},$(layero).find('form').serializeObject());
  			  ajaxSer.dictionLeftEdit(jsonList,function(data){
  				  permissionTable(treeid).draw();
  			  });
  			  layer.close(index);
  			}
  		  },
  		  cancel:function(index){
  			  layer.close(index);
  		  }
  		});
    });
    
    
//  删除
    $(".permissdel").on('click',function(){
    	var del_tIds = "";
        var data_a = $("#datatables tbody .selected input");
        $.each(data_a, function (n) {
            if (n == 0) {
                del_tIds = data_a.eq(n).val();
            } else {
                del_tIds = del_tIds + "," + data_a.eq(n).val();
            }
        })
        if (del_tIds == undefined || $.trim(del_tIds) == "") {
        	layer.msg("请选择一条记录！");
            return false;
        }
    	layer.confirm('确认删除?', {icon: 3, title:'删除'}, function(index){
    		ajaxSer.dictionLeftDel(del_tIds,function(data){
    			if (data.success == true) {
                	layer.msg("需删除数据" + data_a.length + "条，成功:"+ data_a.length + "条！");
                	permissionTable($(".treeidpara").val()).draw();
                } else {
                	layer.msg("删除数据失败");
                }
			});
		  layer.close(index);
		});
    });
    
  //单击行选中事件
    $('#datatables tbody').on('click', 'tr', function () {
    	
    	var table = $('#datatables').DataTable();
        var data = table.row(this).data();
        $(".typeid").val(data.type);
        $('#datatables tr').removeClass('selected');
        $(this).addClass('selected');
        ajaxSer.dictionLeftIsSys(data.type,function(param){
        	if(userIsSys == 1 && param == 0){
        		$(".userhide").hide();
        	}else{
        		$(".userhide").show();
        	}
        });
        dictionaryTable(data.type);
    });
    
//  新增
    $(".dicDetopen").on('click',function(){
    	var typeId = $(".typeid").val();
    	if(typeId != "" && typeId != null){
	    	layer.open({
	    	  title: '新增', 
			  skin: 'demo-class',
			  btn: ['确认', '取消'],
			  content: $(".dicdetform").html(),
			  success: function(layero, index){
				    console.log(layero, index);
				    $(layero).find('#addDicDetRight').validate();
			  },
			  yes:function(index, layero){
				  if($(layero).find('#addDicDetRight').valid()){
					  var jsonList = $(layero).find('form').serializeObject();
					  ajaxSer.dictionRightAdd(typeId, jsonList, function(data){
						  dictionaryTable(typeId).draw();
					  });
					  //这里是回调，点击确认的时候执行的方法，最后一个关闭需要手动调用
					  layer.close(index);
				  }
			  },
			  cancel:function(index){
				  layer.close(index);
			  }
			});
    	}else{
    		layer.msg('请选择左侧菜单！');
    	}
    });
    
//  修改
    $(".dicDetedit").on('click',function(){
    	var choose_a = $('#datatables_detail tbody :checkbox:checked');
        var c_tIds;
        c_tIds = choose_a.eq(0).val();
        if (c_tIds == undefined || $.trim(c_tIds) == "") {
        	layer.msg("请选择一条记录！");
            return false;
        }
        if(choose_a.length != 1){
        	layer.msg("只能选择一条记录！");
            return false;
        }
    	layer.open({
    	  title: '修改', 
  		  skin: 'demo-class',
  		  btn: ['确认', '取消'],
  		  content: $(".dicdetform").html(),
  		  success: function(layero, index){
  			  	//修改之前执行填充操作
  			    $(layero).find('#addDicDetRight').validate();
	  			ajaxSer.dictionRightEditBefore(c_tIds, function(data){
	  				var nameVal = $(layero).find('form input');
					for(var i=0; i<nameVal.length;i++){
						if(data[$(nameVal[i]).attr('name')]){
							$(nameVal[i]).val(data[$(nameVal[i]).attr('name')])
						}
					}
	  			});
  		  },
  		  yes:function(index, layero){
  			if($(layero).find('#addDicDetRight').valid()){
  			  var treeid = $(layero).find('.treeidpara').val();
  			  var jsonList = $.extend({},{'Id':c_tIds},$(layero).find('form').serializeObject());
  			  ajaxSer.dictionRightEdit(jsonList, function(data){
  				  dictionaryTable($(".typeid").val()).draw();
  			  });
  			  layer.close(index);
  			}
  		  },
  		  cancel:function(index){
  			  layer.close(index);
  		  }
  		});
    });
    
    
//  删除
    $(".dicDetdel").on('click',function(){
    	var del_tIds = "";
        var data_a = $("#datatables_detail tbody :checkbox:checked");
        $.each(data_a, function (n) {
            if (n == 0) {
                del_tIds = data_a.eq(n).val();
            } else {
                del_tIds = del_tIds + "," + data_a.eq(n).val();
            }
        })
        if (del_tIds == undefined || $.trim(del_tIds) == "") {
        	layer.msg("请选择一条记录！");
            return false;
        }
    	layer.confirm('确认删除?', {icon: 3, title:'删除'}, function(index){
    		ajaxSer.dictionRightDel(del_tIds, function(data){
    			if (data.success == true) {
                	layer.msg("需删除数据" + data_a.length + "条，成功:"+ data_a.length + "条！");
                	dictionaryTable($(".typeid").val()).draw();
                } else {
                	layer.msg("删除数据失败");
                }
    		});
		    layer.close(index);
		});
    });
    
  //单击行选中事件
    $('#datatables_detail tbody').on('click', 'tr', function () {
        var table = $('#datatables_detail').DataTable();
        var data = table.row(this).data();
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
            $('#check' + data.id).prop("checked", false);
        } else {
            $(this).addClass('selected');
            $('#check' + data.id).prop("checked", true);
        }
    });
});